// CSE571 - server-interpreter.h
// Authors: Jonathan Wald, Guangle Fan        
// Defines the server interpreter, a subclass of the
// abstract interpreter.  


#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>

#include "abstract-interpreter.h"
#include "server-interpreter.h"
#include "async-io-svc-handler.h"
#include "game-manager.h"
#include "player-svc-handler.h"
#include "player-game.h"
#include "game.h"
#include "dealer.h"

using namespace std;

// This is the only thread that mutates list of player games so no
// synchronization is necessary.
void ServerInterpreter::define(string filename) {
  if (filename.empty()) {
    std::cout << "No filename given, not (re)initializing." << std::endl;
    return;
  }
  char* cstr;
  cstr = new char[filename.size() + 1];
  strcpy(cstr, filename.c_str());
  Dealer::instance()->init(1, &cstr);

}

void ServerInterpreter::current(string label, string value) {
  // If label is empty, then call info on the entire server service, otherwise
  // find the specific game service and call info on that.
  Dealer* dealer =  Dealer::instance();
  if(label.empty()) {
    dealer->info(NULL);
    return;
  } else if (value.empty()) {
    map<string, Game *>::iterator giter;
    for (giter = dealer->games.begin(); giter != dealer->games.end(); ++giter) {
      if(label == giter->second->label) {
        giter->second->info(NULL);
        return;
      }
    }
  for (giter = dealer->finished_games.begin(); giter != dealer->finished_games.end(); ++giter) {
    if(label == giter->second->label) {
      giter->second->info(NULL);
      return;
    }
  }

    std::cout << "Label does not identify a service." << std::endl;
    return;
  } else {
    map<string, Game *>::iterator giter;
    for (giter = dealer->games.begin(); giter != dealer->games.end(); ++giter) {
      if(label == giter->second->label) {
        giter->second->label = value;
        return;
      }
    }
    for (giter = dealer->finished_games.begin(); giter != dealer->finished_games.end(); ++giter) {
      if(label == giter->second->label) {
        giter->second->label = value;
	return;
      }
    }

    std::cout << "Label does not identify a service." << std::endl;
    return;
  }
}

void ServerInterpreter::play(string game_type) {
  Dealer* dealer = Dealer::instance();
  if (game_type == "draw") {
    dealer->plays_draw = true;
  } else if (game_type == "holdem") {
    dealer->plays_holdem = true;
  } else if (game_type == "stud") {
    dealer->plays_stud = true;
  }
}

void ServerInterpreter::dontplay(string game_type) {
  Dealer* dealer = Dealer::instance();
  if (game_type == "draw") {
    dealer->plays_draw = false;
  } else if (game_type == "holdem") {
    dealer->plays_holdem = false;
  } else if (game_type == "stud") {
    dealer->plays_stud = false;
  }
}

void ServerInterpreter::suspend(string game) {
  Dealer* dealer = Dealer::instance();
  if(game.empty()) {
    dealer->suspend();
    return;
  }
  map<string, Game *>::iterator giter;
  for (giter = dealer->games.begin(); giter != dealer->games.end(); ++giter) {
    if(game == giter->second->name) {
      giter->second->suspend();
      return;
    }
  }
  for (giter = dealer->finished_games.begin(); giter != dealer->finished_games.end(); ++giter) {
    if(game == giter->second->name) {
      giter->second->suspend();
      return;
    }
  }
  std::cout << "No game with that name exists." << std::endl;
  return;
}

void ServerInterpreter::resume(string game) {
  Dealer* dealer = Dealer::instance();
  if(game.empty()) {
    dealer->resume();
    return;
  }
  map<string, Game *>::iterator giter;
  for (giter = dealer->games.begin(); giter != dealer->games.end(); ++giter) {
    if(game == giter->second->name) {
      giter->second->resume();
      return;
    }
  }
  for (giter = dealer->finished_games.begin(); giter != dealer->finished_games.end(); ++giter) {
    if(game == giter->second->name) {
      giter->second->resume();
      return;
    }
  }
  std::cout << "No game with that name exists." << std::endl;
  return;
}
